package com.feng.config;

import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ParameterType;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    private boolean DEBUG = false;
    private static String PROD_BASE_URL = "www.lytxmd.com/api";
    private static String DEV_BASE_URL = "www.lytxmd.top/api";

    //配置了swagger的Docket 的 bean 实例
//    @Bean
//    public Docket docket() {
//        return new Docket(DocumentationType.SWAGGER_2)
//            .apiInfo(ApiInfo.DEFAULT);
//    }

    //配置 swagger 信息 = apiInfo
//    private ApiInfo apiInfo() {
//        //作者信息
//        Contact contact = new Contact("zhangfeng", "https://www.zhangfeng.vip/", "236661283@qq.com");
//        return new ApiInfo("API文档",
//            "接口信息",
//            "v1.0",
//            "",
//            contact,
//            "Apache 2.0",
//            "http://www.apache.org/licenses/LICENSE-2.0",
//            new ArrayList());
//    }


    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
//            .host(DEBUG?DEV_BASE_URL:PROD_BASE_URL)
            .apiInfo(apiInfo())
            .enable(true) //正式部署时，改为false
            .genericModelSubstitutes(DeferredResult.class)
            .useDefaultResponseMessages(false)
//            .globalOperationParameters(setHeaderToken())//3.0.0中 失效
            .globalRequestParameters(Lists.newArrayList(
                new RequestParameterBuilder()
                    .name("Authorization")
//                    .in("eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImNyZWF0ZWQiOjE1MzAxNzU0NDAyODksImV4cCI6MTUzMDc4MDI0MH0.EvYkFeIojtiAYaGAm0b46YX7BhOMW3QRIgafr7aGtuQVetSBSx2ijXubvnnughntaVl3pkqCq2mpJDCa1awuDw")
                    .in(ParameterType.HEADER)
                    .build())
            )//添加accessToken
            .forCodeGeneration(false)
            .pathMapping("/")
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.feng.api"))
//                .apis(RequestHandlerSelectors.any())
//                .paths(PathSelectors.any())
            .build();
//                .enable(true);
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("Feng Resful API Docs")
            .description("API列表")
            .version("1.0")
            .build();
    }

    private List<Parameter> setHeaderToken() {
        //添加head参数start
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        Parameter tokenHeader = tokenPar.name("Authorization")
            //仅限测试使用,该Token有效期为7天
            .defaultValue("eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImNyZWF0ZWQiOjE1MzAxNzU0NDAyODksImV4cCI6MTUzMDc4MDI0MH0.EvYkFeIojtiAYaGAm0b46YX7BhOMW3QRIgafr7aGtuQVetSBSx2ijXubvnnughntaVl3pkqCq2mpJDCa1awuDw")
            .description("令牌")
            .modelRef(new ModelRef("string"))
            .parameterType("header")
            .required(false)
            .build();
        pars.add(tokenHeader);

//        Parameter openIdHeader = tokenPar.name("openId")
//            //仅限测试使用,该Token有效期为7天
//            .defaultValue("eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImNyZWF0ZWQiOjE1MzAxNzU0NDAyODksImV4cCI6MTUzMDc4MDI0MH0.EvYkFeIojtiAYaGAm0b46YX7BhOMW3QRIgafr7aGtuQVetSBSx2ijXubvnnughntaVl3pkqCq2mpJDCa1awuDw")
//            .description("微信openId")
//            .modelRef(new ModelRef("string"))
//            .parameterType("header")
//            .required(false)
//            .build();
//        pars.add(openIdHeader);
        //添加head参数end
        return pars;
    }
}